package com.hspedu.collection_;
/*
 *@auther will
 *@version 1.0
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
       ArrayList<Object> col =new ArrayList<Object>();
        col.add(new Book("三国演义","罗贯中",10.1));
        col.add(new Book("小李飞刀","古龙",5.1));
        col.add(new Book("红楼梦","曹雪芹",34.6));
        //System.out.println("col="+col);
        //遍历col集合
        //1. 先得到 col 对应的迭代器
        Iterator iterator = col.iterator();
        while (iterator.hasNext()){
            //返回下一个元素，类型是Object
            Object next = iterator.next();
            System.out.println(next);
        }
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }

        //如果希望再次遍历，需要重置我们的迭代器
        iterator = col.iterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            System.out.println(obj);
        }
    }
}
class Book{
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}